Conditions | 19 |
Paths | 53 |
Total Lines | 77 |
Lines | 21 |
Ratio | 27.27 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like Coordinates.fromString often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /*jslint |
||
47 | Coordinates.fromString = function (coordsString) { |
||
48 | 'use strict'; |
||
49 | |||
50 | coordsString = coordsString.trim(); |
||
51 | |||
52 | var matches, pattern, |
||
53 | lat, lat_sign, lat_d, lat_m, lat_s, |
||
54 | lng, lng_sign, lng_d, lng_m, lng_s; |
||
55 | View Code Duplication | ||
56 | // H DDD MM.MMM |
||
57 | pattern = /^[^A-Za-z0-9.\-]*([ns])[^A-Za-z0-9.\-]*(\d+)[^A-Za-z0-9.\-]+([\d\.]+)[^A-Za-z0-9.\-]+([we])[^A-Za-z0-9.\-]*(\d+)[^A-Za-z0-9.\-]+([\d\.]+)[^A-Za-z0-9.\-]*$/i; |
||
58 | matches = coordsString.match(pattern); |
||
59 | if (matches) { |
||
60 | lat_sign = (matches[1] === 's' || matches[1] === 'S') ? -1 : 1; |
||
61 | lng_sign = (matches[4] === 'w' || matches[4] === 'W') ? -1 : 1; |
||
62 | |||
63 | lat_d = parseFloat(matches[2]); |
||
64 | lat_m = parseFloat(matches[3]); |
||
65 | |||
66 | lng_d = parseFloat(matches[5]); |
||
67 | lng_m = parseFloat(matches[6]); |
||
68 | |||
69 | lat = lat_sign * (lat_d + (lat_m / 60.0)); |
||
70 | lng = lng_sign * (lng_d + (lng_m / 60.0)); |
||
71 | |||
72 | return new google.maps.LatLng(lat, lng); |
||
73 | } |
||
74 | |||
75 | // H DDD MM SS.SSS |
||
76 | pattern = /^[^A-Za-z0-9.\-]*([ns])[^A-Za-z0-9.\-]*(\d+)[^A-Za-z0-9.\-]+(\d+)[^A-Za-z0-9.\-]+([\d\.]+)[^A-Za-z0-9.\-]+([we])[^A-Za-z0-9.\-]*(\d+)[^A-Za-z0-9.\-]+(\d+)[^A-Za-z0-9.\-]+([\d\.]+)[^A-Za-z0-9.\-]*$/i; |
||
77 | matches = coordsString.match(pattern); |
||
78 | if (matches) { |
||
79 | lat_sign = (matches[1] === 's' || matches[1] === 'S') ? -1 : 1; |
||
80 | lng_sign = (matches[5] === 'w' || matches[5] === 'W') ? -1 : 1; |
||
81 | |||
82 | lat_d = parseFloat(matches[2]); |
||
83 | lat_m = parseFloat(matches[3]); |
||
84 | lat_s = parseFloat(matches[4]); |
||
85 | |||
86 | lng_d = parseFloat(matches[6]); |
||
87 | lng_m = parseFloat(matches[7]); |
||
88 | lng_s = parseFloat(matches[8]); |
||
89 | |||
90 | lat = lat_sign * (lat_d + (lat_m / 60.0) + (lat_s / 3600.0)); |
||
91 | lng = lng_sign * (lng_d + (lng_m / 60.0) + (lng_s / 3600.0)); |
||
92 | |||
93 | return new google.maps.LatLng(lat, lng); |
||
94 | } |
||
95 | |||
96 | // H DDD.DDDDD |
||
97 | pattern = /^[^A-Za-z0-9.\-]*([ns])[^A-Za-z0-9.\-]*([\d\.]+)[^A-Za-z0-9.\-]+([we])[^A-Za-z0-9.\-]*([\d\.]+)[^A-Za-z0-9.\-]*$/i; |
||
98 | matches = coordsString.match(pattern); |
||
99 | if (matches) { |
||
100 | lat_sign = (matches[1] === 's' || matches[1] === 'S') ? -1 : 1; |
||
101 | lng_sign = (matches[3] === 'w' || matches[3] === 'W') ? -1 : 1; |
||
102 | |||
103 | lat = lat_sign * parseFloat(matches[2]); |
||
104 | lng = lng_sign * parseFloat(matches[4]); |
||
105 | |||
106 | return new google.maps.LatLng(lat, lng); |
||
107 | } |
||
108 | |||
109 | // H DDD.DDDDD |
||
110 | pattern = /^[^A-Za-z0-9.\-]*(-?)([\d\.]+)[^A-Za-z0-9.\-]+(-?)([\d\.]+)[^A-Za-z0-9.\-]*$/i; |
||
111 | matches = coordsString.match(pattern); |
||
112 | if (matches) { |
||
113 | lat_sign = (matches[1] === '-') ? -1 : 1; |
||
114 | lng_sign = (matches[3] === '-') ? -1 : 1; |
||
115 | |||
116 | lat = lat_sign * parseFloat(matches[2]); |
||
117 | lng = lng_sign * parseFloat(matches[4]); |
||
118 | |||
119 | return new google.maps.LatLng(lat, lng); |
||
120 | } |
||
121 | |||
122 | return null; |
||
123 | }; |
||
124 | |||
317 |